Dart Operators – Detailed Notes
Operators are special symbols or keywords used to perform operations on values and variables. They are an essential part of Dart programming because they allow developers to perform calculations, compare values, combine conditions, assign values, check types, and work with expressions.
JustAcademy's Flutter curriculum includes Dart Programming Fundamentals, covering variables, data types, operators, control statements, functions, OOP, collections, and asynchronous programming. :contentReference[oaicite:0]{index=0}
1. What are Operators?
An operator is a symbol or keyword that tells Dart to perform a particular operation.
For example:
int a = 10;
int b = 5;
int result = a + b;
print(result);
Output:
15
In this example, + is an arithmetic operator used to add two values.
2. Types of Operators in Dart
Dart provides several categories of operators:
- Arithmetic Operators
- Unary Operators
- Assignment Operators
- Relational / Comparison Operators
- Logical Operators
- Type Test Operators
- Conditional Operators
- Null-Aware Operators
- Bitwise and Shift Operators
- Cascade Notation
3. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
| Operator |
Name |
Example |
Result |
+ |
Addition |
10 + 5 |
15 |
- |
Subtraction |
10 - 5 |
5 |
* |
Multiplication |
10 * 5 |
50 |
/ |
Division |
10 / 5 |
2.0 |
~/ |
Integer division |
10 ~/ 3 |
3 |
% |
Modulo / Remainder |
10 % 3 |
1 |
Example
void main() {
int a = 20;
int b = 6;
print(a + b);
print(a - b);
print(a * b);
print(a / b);
print(a ~/ b);
print(a % b);
}
4. Addition Operator (+)
The + operator is used for addition. It can also be used for String concatenation.
int a = 10;
int b = 20;
int result = a + b;
print(result);
Output:
30
String Example
String firstName = "Rahul";
String lastName = "Sharma";
String fullName = firstName + " " + lastName;
print(fullName);
5. Subtraction Operator (-)
int price = 1000;
int discount = 200;
int finalPrice = price - discount;
print(finalPrice);
Output:
800
6. Multiplication Operator (*)
double price = 500;
int quantity = 3;
double total = price * quantity;
print(total);
Output:
1500.0
7. Division Operator (/)
The / operator performs division and produces a numeric result suitable for fractional values.
int totalMarks = 450;
int subjects = 5;
double average = totalMarks / subjects;
print(average);
8. Integer Division Operator (~/)
The ~/ operator performs integer division.
int result = 10 ~/ 3;
print(result);
Output:
3
The fractional part is discarded.
9. Modulo Operator (%)
The modulo operator returns the remainder after division.
int remainder = 10 % 3;
print(remainder);
Output:
1
Practical Example – Even and Odd Numbers
int number = 20;
if (number % 2 == 0) {
print("Even number");
} else {
print("Odd number");
}
10. Unary Operators
Unary operators work with a single operand.
Unary Minus (-)
int number = 10;
print(-number);
Output:
-10
Increment Operator
int count = 5;
count++;
print(count);
Output:
6
Decrement Operator
int count = 5;
count--;
print(count);
Output:
4
11. Prefix and Postfix Operators
Increment and decrement operators can be used before or after an expression.
Postfix
int a = 5;
print(a++);
print(a);
The current value is used first, then the variable is incremented.
Prefix
int a = 5;
print(++a);
The variable is incremented before its value is used in the expression.
12. Assignment Operators
Assignment operators are used to assign or update values stored in variables.
| Operator |
Example |
Equivalent |
= |
a = 10 |
Assign 10 |
+= |
a += 5 |
a = a + 5 |
-= |
a -= 5 |
a = a - 5 |
*= |
a *= 5 |
a = a * 5 |
/= |
a /= 5 |
a = a / 5 |
~/= |
a ~/= 5 |
a = a ~/ 5 |
%= |
a %= 5 |
a = a % 5 |
13. Basic Assignment (=)
int age = 25;
age = 30;
print(age);
The = operator assigns the value on the right to the variable on the left.
14. Addition Assignment (+=)
int score = 50;
score += 10;
print(score);
Equivalent to:
score = score + 10;
15. Subtraction Assignment (-=)
int balance = 1000;
balance -= 250;
print(balance);
Output:
750
16. Multiplication Assignment (*=)
int number = 5;
number *= 4;
print(number);
Output:
20
17. Division Assignment (/=)
double price = 1000;
price /= 2;
print(price);
Output:
500.0
18. Comparison Operators
Comparison operators compare two values and produce a Boolean result: true or false.
| Operator |
Meaning |
Example |
== |
Equal to |
a == b |
!= |
Not equal to |
a != b |
> |
Greater than |
a > b |
< |
Less than |
a < b |
>= |
Greater than or equal to |
a >= b |
<= |
Less than or equal to |
a <= b |
Example
int age = 20;
print(age == 20);
print(age != 18);
print(age > 18);
print(age < 30);
print(age >= 20);
print(age <= 20);
19. Equal To (==)
The == operator checks whether two values are equal.
int a = 10;
int b = 10;
print(a == b);
Output:
true
20. Not Equal To (!=)
int age = 25;
print(age != 18);
Output:
true
21. Greater Than and Less Than
int marks = 75;
print(marks > 50);
print(marks < 90);
22. Greater Than or Equal To
int marks = 40;
print(marks >= 40);
Output:
true
23. Less Than or Equal To
int age = 18;
print(age <= 18);
Output:
true
24. Logical Operators
Logical operators are used to combine or invert Boolean expressions.
| Operator |
Name |
Meaning |
&& |
Logical AND |
Both conditions must be true |
|| |
Logical OR |
At least one condition must be true |
! |
Logical NOT |
Reverses a Boolean value |
25. Logical AND (&&)
The && operator returns true only when both conditions are true.
int age = 25;
bool hasLicense = true;
bool canDrive = age >= 18 && hasLicense;
print(canDrive);
Output:
true
Example with Login
String username = "admin";
String password = "1234";
if (username == "admin" && password == "1234") {
print("Login successful");
}
26. Logical OR (||)
The || operator returns true when at least one condition is true.
bool isAdmin = false;
bool isManager = true;
if (isAdmin || isManager) {
print("Access granted");
}
27. Logical NOT (!)
The ! operator reverses a Boolean value.
bool isLoggedIn = false;
print(!isLoggedIn);
Output:
true
28. Combining Logical Operators
int age = 25;
bool isStudent = true;
bool hasId = true;
if ((age >= 18 && isStudent) || hasId) {
print("Eligible");
}
Parentheses can make complex expressions easier to read and control the intended grouping.
29. Conditional Operator (Ternary Operator)
The ternary operator is a short way to choose between two expressions based on a condition.
condition ? valueIfTrue : valueIfFalse
Example
int age = 20;
String result = age >= 18 ? "Adult" : "Minor";
print(result);
Output:
Adult
Another Example
double marks = 75;
String result = marks >= 40 ? "Pass" : "Fail";
print(result);
30. Null-Aware Operators
Dart provides operators that make it easier to work with nullable values.
Important null-aware operators include:
??
??=
?.
! for asserting that a nullable value is non-null when you know that to be true
31. Null-Coalescing Operator (??)
The ?? operator returns the value on the left when it is not null; otherwise, it returns the value on the right.
String? name;
String displayName = name ?? "Guest";
print(displayName);
Output:
Guest
Example
String? username = "Rahul";
String displayName = username ?? "Guest";
print(displayName);
Output:
Rahul
32. Null-Aware Assignment (??=)
The ??= operator assigns a value only when the variable is currently null.
String? name;
name ??= "Guest";
print(name);
Output:
Guest
If name already contains a value, ??= does not replace it.
String? name = "Rahul";
name ??= "Guest";
print(name);
Output:
Rahul
33. Null-Aware Access Operator (?.)
The ?. operator allows an operation to be performed only when the object is not null.
String? name = "Rahul";
print(name?.length);
If name is null, the expression evaluates to null instead of attempting to access a property on a null object.
34. Null Assertion Operator (!)
The ! operator tells Dart that you know a nullable expression is not null at that point.
String? name = "Rahul";
print(name!.length);
This should only be used when you are certain the value is not null. Otherwise, a runtime error can occur.
35. Type Test Operators
Dart provides operators for checking and working with types.
| Operator |
Purpose |
is |
Checks whether a value is of a particular type |
is! |
Checks whether a value is not of a particular type |
as |
Performs an explicit type cast |
is Example
var value = 100;
if (value is int) {
print("Value is an integer");
}
is! Example
var value = "Hello";
if (value is! int) {
print("Value is not an integer");
}
as Example
Object value = "Hello Dart";
String text = value as String;
print(text);
Type casts should be used when the type relationship is known and valid.
36. Bitwise Operators
Dart also provides bitwise operators for working with the binary representation of integer values.
| Operator |
Name |
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR |
~ |
Bitwise NOT |
Example
int a = 5;
int b = 3;
print(a & b);
print(a | b);
print(a ^ b);
Bitwise operations are generally more specialized than everyday arithmetic and logical operations.
37. Shift Operators
Shift operators move the bits of an integer value.
<< – left shift
>> – right shift
>>> – unsigned right shift
Example
int value = 4;
print(value << 1);
print(value >> 1);
38. Cascade Notation
Cascade notation allows multiple operations to be performed on the same object.
Dart uses .. and ?.. for cascade operations.
Example
var numbers = []
..add(10)
..add(20)
..add(30);
print(numbers);
Output:
[10, 20, 30]
Cascades can make chained object configuration more concise.
39. Operator Precedence
When an expression contains multiple operators, Dart evaluates them according to operator precedence and associativity.
int result = 10 + 5 * 2;
print(result);
Multiplication is evaluated before addition, so the result is:
20
Using Parentheses
int result = (10 + 5) * 2;
print(result);
Output:
30
Parentheses make the intended order explicit and improve readability.
40. Practical Example – Shopping Cart
void main() {
double productPrice = 1000;
int quantity = 3;
double discount = 200;
double subtotal = productPrice * quantity;
double finalPrice = subtotal - discount;
print("Subtotal: $subtotal");
print("Final Price: $finalPrice");
}
This example uses multiplication and subtraction operators to calculate a shopping-cart total.
41. Practical Example – Student Result
void main() {
int marks = 75;
bool passed = marks >= 40;
String result = passed ? "Pass" : "Fail";
print("Marks: $marks");
print("Result: $result");
}
This example combines a comparison operator with a ternary conditional operator.
42. Practical Example – Login Validation
void main() {
String username = "admin";
String password = "12345";
bool validUsername = username == "admin";
bool validPassword = password == "12345";
if (validUsername && validPassword) {
print("Login successful");
} else {
print("Invalid credentials");
}
}
This example uses the equality operator and logical AND operator.
43. Practical Example – Age Eligibility
void main() {
int age = 21;
if (age >= 18) {
print("Eligible");
} else {
print("Not eligible");
}
}
The >= comparison operator checks whether the age is at least 18.
44. Practical Example – Nullable User Name
void main() {
String? username;
String displayName = username ?? "Guest";
print("Welcome, $displayName");
}
The ?? operator provides a fallback value when the username is null.
45. Operators in Flutter Applications
Dart operators are used extensively when developing Flutter applications. For example, they can be used for calculating totals, validating forms, checking application state, selecting UI text, handling nullable API data, and evaluating conditions.
JustAcademy's Flutter curriculum progresses from Dart fundamentals into Flutter widgets, navigation, state management, API integration, Firebase, and real-world projects. :contentReference[oaicite:1]{index=1}
Example
double price = 500;
int quantity = 2;
double total = price * quantity;
bool hasItems = quantity > 0;
String message = hasItems
? "Items available"
: "Cart is empty";
print(total);
print(message);
46. Common Operator Mistakes
Mistake 1 – Confusing = and ==
int age = 20;
// Assignment
age = 25;
// Comparison
print(age == 25);
= assigns a value, while == compares values.
Mistake 2 – Using & Instead of &&
bool result = age > 18 && age < 60;
Use && for logical AND conditions.
Mistake 3 – Forgetting Null Safety
String? name;
String displayName = name ?? "Guest";
Mistake 4 – Unnecessary Null Assertion
String? name;
// print(name!.length);
Do not use ! unless you are certain the value is not null.
47. Operator Quick Reference
| Category |
Operators |
Purpose |
| Arithmetic |
+ - * / ~/ % |
Mathematical calculations |
| Unary |
- ++ -- |
Single-value operations |
| Assignment |
= += -= *= /= ~/= %= |
Assign/update values |
| Comparison |
== != > < >= <= |
Compare values |
| Logical |
&& || ! |
Combine Boolean expressions |
| Conditional |
? : |
Select between two expressions |
| Null-aware |
?? ??= ?. ! |
Work with nullable values |
| Type test |
is is! as |
Check/cast types |
| Bitwise |
& | ^ ~ |
Bit-level operations |
| Shift |
<< >> >>> |
Shift binary bits |
| Cascade |
.. ?.. |
Perform multiple operations on an object |
48. Best Practices for Using Operators
- Use parentheses when they make complex expressions easier to understand.
- Use
== for comparison and = for assignment.
- Use logical operators to combine Boolean conditions clearly.
- Use
?? when a nullable value needs a fallback.
- Avoid unnecessary use of the null assertion operator
!.
- Use the ternary operator for simple conditional expressions.
- Break complicated expressions into smaller variables when readability suffers.
- Use appropriate arithmetic operators for calculations rather than duplicating logic.
- Understand operator precedence when combining several operators.
49. Practice Exercises
- Create two integers and perform addition, subtraction, multiplication, and division.
- Use the modulo operator to determine whether a number is even or odd.
- Use
+= to increase a score.
- Use comparison operators to check whether a student has passed.
- Use
&& to validate two conditions.
- Use
|| to check whether a user is an admin or manager.
- Use the
! operator to reverse a Boolean value.
- Use the ternary operator to display "Pass" or "Fail".
- Use
?? to provide a default username.
- Use
??= to assign a default value only when a variable is null.
- Use
is to check the type of a variable.
- Create a small shopping-cart calculation using multiple operators.
50. Complete Dart Example
void main() {
double price = 1000;
int quantity = 3;
// Arithmetic operators
double subtotal = price * quantity;
double discount = 200;
double finalPrice = subtotal - discount;
// Comparison operator
bool isExpensive = finalPrice > 2000;
// Logical operator
bool hasItems = quantity > 0;
bool canCheckout = hasItems && finalPrice > 0;
// Ternary operator
String checkoutMessage =
canCheckout ? "Ready for checkout" : "Cart is empty";
// Null-aware operator
String? coupon;
String couponMessage = coupon ?? "No coupon applied";
print("Subtotal: $subtotal");
print("Final Price: $finalPrice");
print("Expensive: $isExpensive");
print("Can Checkout: $canCheckout");
print("Message: $checkoutMessage");
print("Coupon: $couponMessage");
}
51. Key Takeaways
- Operators perform operations on values and expressions.
- Arithmetic operators are used for mathematical calculations.
- Assignment operators assign or update variable values.
- Comparison operators return Boolean results.
- Logical operators combine Boolean conditions.
- The ternary operator provides a compact conditional expression.
- Null-aware operators make working with nullable values easier.
- Type operators allow type checking and casting.
- Bitwise and shift operators work with integer bit representations.
- Cascade notation allows multiple operations on the same object.
- Operators are fundamental to Dart programming and are used throughout Flutter application development. :contentReference[oaicite:2]{index=2}
Learn Flutter with JustAcademy
JustAcademy's Flutter training covers Dart programming fundamentals, including variables, data types, operators, control statements, functions, OOP, collections, and asynchronous programming. :contentReference[oaicite:3]{index=3}
Explore the complete course:
JustAcademy Flutter Training
Register for the course demo:
JustAcademy Course Demo Registration
Learning Dart operators provides an important foundation for writing conditions, calculations, validations, data-handling logic, and interactive Flutter applications.